<template>
  <div class="_wz_cent_main _wz_ExamTestReco">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>
      <Form :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="考试名称">
          <i-input v-model="formDataSearch.planName"  placeholder="考试名称" clearable />
        </FormItem>
        <FormItem label="考试方向">
          <i-select v-model="formDataSearch.planDirection" filterable placeholder="考试方向" clearable>
            <Option v-for="item in optionsData" :value="item.code" :key="item.code">{{ item.value }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="考试进度" v-if="searchType">
          <i-select v-model="formDataSearch.finishState" filterable placeholder="发布状态" clearable>
            <Option value="0">未开始</Option>
            <Option value="2">进行中</Option>
            <Option value="1">已完成</Option>
          </i-select>
        </FormItem>
        <FormItem label="年月检索" v-if="searchType">
          <DatePicker type="month" v-model="formDataSearch.wzYearMonth" placeholder="请选择年月"></DatePicker>
        </FormItem>
        <FormItem label="发布状态" v-if="searchType">
          <i-select v-model="formDataSearch.releaseState" filterable placeholder="发布状态" clearable>
            <Option value="1">已发布</Option>
            <Option value="0">未发布</Option>
            <Option value="2">发布中</Option>
            <Option value="-1">发布失败</Option>
          </i-select>
        </FormItem>
        <FormItem label="运输企业" v-if="searchType">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="examAdd">添加</Button>
        <Button type="success" @click="ExportData">导出</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>
    <Drawer :title="formTitle" v-model="formOperation" width="800" :draggable="true" :mask-closable="false" :styles="formOperationStyles">
      <ExamTestAdd v-if="formOperation" :selectData="selectData" @closeFun="closeFun"></ExamTestAdd>
    </Drawer>


    <Drawer :title="examPlanName" v-model="examinationDetails" width="700" :draggable="true">
      <materialExam v-if="examinationDetails" :materialData="materialData"></materialExam>
    </Drawer>


    <Drawer title="考试列表" v-model="examListType" width="1100" :draggable="true" :mask-closable="false">
      <examList v-if="examListType" :selectData="selectData"></examList>
    </Drawer>
    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>
  import ExamTestAdd from './ExamTestAdd.vue'
  import materialExam from '../public/materialExam.vue'
  import wzTbale from "../../../components/table/table";
  import examList from './examList.vue'

  export default {
    components: {
      ExamTestAdd,//发布考试
      materialExam,//试题
      wzTbale,//列表
      examList,
    },
    data() {
      return {
        spinShow:false,//本页加载
        searchType:false,//搜索条件
        examListType:false,
        subDeptList: [],//运输企业
        optionsData: [],//考试方向数据
        examPlanName: "",//试卷名称
        formDataSearch: {},//搜索表单数据
        //发布考试
        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        formTitle: "",//表单名称
        selectData: {},//选择数据

        //试卷
        examinationDetails: false,//弹出层状态
        //查看试卷数据id
        materialData: {},

        tableData: {
          url: this.apiUrl.educateExamPlanExamPlanPageList,//请求地址
          method: 'post',//请求方式
          where: {},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表头数据
            {type: 'index', width: 70, align: 'center', title: '序号'},
            {
              title: '考试周期', minWidth: 120, render: (h, params) => {
                return h('span', params.row.examPlanCreateYear + '年' + params.row.examPlanCreateMonth + '月');
              }
            },
            {title: '考试测验', minWidth: 200, key: 'examPlanName'},
            {title: '测验试卷', minWidth: 180, key: 'materialExamName'},
            {title: '考试方向', minWidth: 100, key: 'examPlanDirectionName'},
            {
              title: '发布状态', minWidth: 140, render: (h, params) => {
                //  0：未开始，1：已完成，-1：进行中
                if (params.row.examPlanRelease == 1) {
                  return h('Tag', {props: {type: 'dot',color: 'success'}}, '已发布')
                } else if (params.row.examPlanRelease == 2) {
                  return h('Tag', {props: {type: 'dot',color: 'primary'}}, '发布中')
                } else if (params.row.examPlanRelease == 0) {
                  return h('Tag', {props: {type: 'dot',color: 'warning'}}, '未发布')
                } else if (params.row.examPlanRelease == -1) {
                  return h('Tag', {props: {type: 'dot',color: 'error'}}, '发布失败')
                }
              }
            },
            {
              title: '考试类型', minWidth: 140, render: (h, params) => {
                //  0 考试 1 随堂
                if (params.row.examPlanType == 0) {
                  return h('Tag', {props: {type: 'dot',color: 'success'}}, '考试测验')
                } else {
                  return h('Tag', {props: {type: 'dot',color: 'warning'}}, '随堂测验')
                }
              }
            },
            {
              title: '考试人数', minWidth: 100, render: (h, params) => {
                return h('span', params.row.examPlanFinishNumber + '/' + params.row.examPlanNumber + '人');
              }
            },
            {
              title: '及格分数', minWidth: 100, render: (h, params) => {
                return h('span',  params.row.examPlanPassScore + ' / ' + params.row.examPlanTotalScore + '分');
              }
            },
            {title: '开始时间', minWidth: 100, key: 'examPlanBeginDate'},
            {title: '结束时间', minWidth: 100, key: 'examPlanEndDate'},
            {title: '运输企业', minWidth: 260, key: 'deptName'},
            {
              title: '操作', fixed: 'right', width: 120,
              render: (h, params) => {
                let operationButtons = [];

                //发布
                if (params.row.examPlanRelease == 0) {
                    operationButtons.push(h('span', {
                      style: {"color": "#2d8cf0", "cursor": "pointer"},
                      on: {
                        click: () => {
                          let that = this;
                          that.$Modal.confirm({
                            title: '发布提醒',
                            content: '确定要发布选中的考试吗?已经发布,不可以修改删除操作',
                            okText: '发布',
                            cancelText: '修改',
                            onOk: () => {
                              that.spinShow = true;
                              that.axios.post(that.apiUrl.educateExamPlanExamPlanRelease, {uuid: params.row.uuid}).then(res => {
                                that.spinShow = false;
                                if (!!res) {
                                  that.$Message.success('发布成功');
                                  that.$refs.wzTbale.tableLoadData();
                                }
                              }).catch(err => {
                                console.log('失败', err);
                              })
                            },
                            onCancel: () => {
                              that.selectData = JSON.parse(JSON.stringify(data));
                              that.formOperation = true;
                            }
                          });
                        }
                      }
                    }, '发布'))
                }


                if (params.row.examPlanRelease == 1) {
                  operationButtons.push(h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.examListType = true;
                      }
                    }
                  }, '列表'))
                }

                var buttonList = [];

                //进度刷新
                if (params.row.examPlanRelease == 1) {
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        let that = this;
                        that.$Modal.confirm({
                          title: '进度刷新提醒',
                          content: '确定要进度刷新选中的数据？',
                          onOk: () => {
                            that.spinShow = true;
                            that.axios.post(that.apiUrl.educateExamPlanExamPlanRefresh, {uuid: params.row.uuid}).then(res => {
                              that.spinShow = false;
                              if (!!res) {
                                that.$Message.success('进度刷新成功');
                                that.$refs.wzTbale.tableLoadData();
                              }
                            }).catch(err => {
                              console.log('失败', err);
                            })
                          },
                          onCancel: () => {
                            //点击取消
                          }
                        });
                      }
                    }
                  }, '进度刷新'))
                }

                if (params.row.trainCoursePlanRelease != 0) {

                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.materialData.id = params.row.materialExamUuid
                        this.examPlanName = params.row.materialExamName
                        this.examinationDetails = true;
                      }
                    }
                  }, '试卷'))
                }

                //修改
                if (params.row.examPlanRelease != 1) {
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.formTitle = "修改";
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.formOperation = true;
                      }
                    }
                  }, '修改'))
                }

                //删除
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.examrem(params.row)
                    }
                  }
                }, '删除'))





                if (buttonList.length > 0) {
                  if(operationButtons.length > 0){
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                    [
                      h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                      h('DropdownMenu', {slot: "list"}, buttonList)
                    ])
                  )
                }
                return operationButtons;

              }
            }
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },

      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      let that = this;


      //运输企业
      that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })

      //加载方向
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'training_direction'}).then(res => {
        if (!!res) {
          that.optionsData = res.data.data
        }
      }).catch(err => {
        console.log('失败', err);
      })
    },
    methods: {//执行的方法
      //搜索
      searchSubmit() {
        this.formDataSearch.createYear = !!this.formDataSearch.wzYearMonth ? this.public.timeFormatting(this.formDataSearch.wzYearMonth, "yyyy") :'';
        this.formDataSearch.createMonth = !!this.formDataSearch.wzYearMonth ? this.public.timeFormatting(this.formDataSearch.wzYearMonth, "MM") : '';
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      //关闭 弹出框刷新列表
      closeFun(data) {
        if (data) {//添加修改 刷新表单
          this.$refs.wzTbale.tableLoadData();
        }
        this.formOperation = false;
      },
      //添加考试
      examAdd() {
        this.selectData = {};
        this.formTitle = "添加";
        this.formOperation = true;
      },
      //删除考试
      examrem(data) {
        let that = this;
        that.$Modal.confirm({
          title: '删除提醒',
          content: '确定要删除选中的考试？',
          onOk: () => {
            that.spinShow = true;//加载
            that.axios.post(that.apiUrl.educateExamPlanExamPlanDelete, {uuid: data.uuid}).then(res => {
              that.spinShow = false;//加载
              if (!!res) {
                that.$Message.success('删除成功');
                that.$refs.wzTbale.tableLoadData();
              }
            }).catch(err => {
              console.log('失败', err);
            })
          },
          onCancel: () => {
            //点击取消
          }
        });
      },
      //查看试卷
      seeExam() {
        let that = this;
        if (!!that.selectData.id) {
        } else {
          that.$Message.warning('请选择一个考试查看');
        }
      },
      //导出
      ExportData(){
        var that = this;
        that.axios.post(that.apiUrl.educateExamPlanExamPlanExport,this.formDataSearch).then(res => {
          that.spinShow = false;
          if (!!res) {
            window.open(that.apiUrl.imgUrl + '/' + res.data.data)
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
    },

    watch: {//监听

    },


    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_ExamTestReco {
    height: 100%;
    display: flex;
    flex-direction: column;
  }
</style>
